import re
import time
import calendar
import datetime


def sep(lable=''):
    '''Utility function to print the separator line.'''
    print('-' * 16, lable, '-' * 16, sep='')


# 1.	正则化处理
# (1)	正则表达式匹配，有以下字符串
test_time = '大宝的生日是2014-12-12 ,是个可爱的小宝贝.二宝的生日是2016-12-21 ,好可爱的.'

# ①	使用正则匹配出大宝的生日和二宝的生日
sep('①	使用正则匹配出大宝的生日和二宝的生日')
xregexp = re.compile(r'(\d{4}-\d{2}-\d{2})')
xdates = xregexp.findall(test_time)
print(xdates)

# ②	计算大宝和二宝生日相差多少天
sep('# ②	计算大宝和二宝生日相差多少天')
date1str = xdates[0]
date2str = xdates[1]
date1stime = time.strptime(date1str, '%Y-%m-%d')
date2stime = time.strptime(date2str, '%Y-%m-%d')
date1time = time.mktime(date1stime)
date2time = time.mktime(date2stime)
diff = abs(date1time-date2time)
days = int(diff / (3600 * 24))
print(days)

sep('by datetime and timedelta')
dt1 = datetime.datetime.strptime(date1str, '%Y-%m-%d')
dt2 = datetime.datetime.strptime(date2str, '%Y-%m-%d')
delta = dt1 - dt2
secs = abs(delta.total_seconds())
days = int(secs / (3600 * 24))
print(days)

# ③	查看大宝和二宝出生当天是星期几
sep('③	查看大宝和二宝出生当天是星期几')
w = list('一二三四五六日')
print(f'大宝出生当天是星期{w[date1stime.tm_wday]}')
print(f'二宝出生当天是星期{w[date2stime.tm_wday]}')
# ④	查看大宝和小宝出生当月的月历
sep('④	查看大宝和小宝出生当月的月历')
print('大宝出生当月的月历')
print(date1stime)
calendar.prmonth(date1stime.tm_year, date1stime.tm_mon)
print('小宝出生当月的月历')
calendar.prmonth(date2stime.tm_year, date2stime.tm_mon)
